package com.redstars.generate.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.entity.SysFieldType;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.utils.PageUtils;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysFieldTypeConvert;
import com.redstars.generate.query.SysFieldTypeQuery;
import com.redstars.generate.vo.SysFieldTypeVo;
import com.redstars.generate.dao.SysFieldTypeDao;
import com.redstars.generate.service.SysFieldTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import cn.hutool.core.util.ObjectUtil;

/**
 * 字段类型管理
 *
 * @author zhouhx 
 * @since 1.0.0 2022-10-05
 */
@Service
@AllArgsConstructor
public class SysFieldTypeServiceImpl extends ServiceImpl<SysFieldTypeDao, SysFieldType> implements SysFieldTypeService {

    @Override
    public PageResult<SysFieldTypeVo> page(SysFieldTypeQuery query) {
        LambdaQueryWrapper<SysFieldType> wrapper = getWrapper(query);
        PageUtils<SysFieldType> pageUtils = new PageUtils<>();
        IPage<SysFieldType> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page,SysFieldTypeVo.class);
    }

    private LambdaQueryWrapper<SysFieldType> getWrapper(SysFieldTypeQuery query){
        LambdaQueryWrapper<SysFieldType> wrapper = Wrappers.lambdaQuery();
        if(StrUtil.isNotBlank(query.getAttrType())){
            wrapper.like(SysFieldType::getAttrType,query.getAttrType());
        }
        if(StrUtil.isNotBlank(query.getColumnType())){
            wrapper.like(SysFieldType::getColumnType,query.getColumnType());
        }
        return wrapper;
    }

    @Override
    public void save(SysFieldTypeVo vo) {
        SysFieldType entity = SysFieldTypeConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysFieldTypeVo vo) {
        SysFieldType entity = SysFieldTypeConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public SysFieldTypeVo detail(Long id) {
        SysFieldType detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
            throw new RedstarsException("记录不存在");
        }
        return SysFieldTypeConvert.INSTANCE.convert(detail);
    }

    @Override
    public Set<String> getTypeList() {
        return this.baseMapper.list();
    }

    @Override
    public Map<String, SysFieldType> getMap() {
        List<SysFieldType> list = baseMapper.selectList(null);
        Map<String, SysFieldType> map = new LinkedHashMap<>(list.size());
        for (SysFieldType entity : list) {
            map.put(entity.getColumnType().toLowerCase(), entity);
        }
        return map;
    }

    @Override
    public Set<String> getPackageByTableId(Long tableId) {
        Set<String> importList = baseMapper.getPackageByTableId(tableId);

        return importList.stream().filter(StrUtil::isNotBlank).collect(Collectors.toSet());
    }
}